<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>九、BOM和DOM | HJ BLOG</title><meta name="keywords" content="JavaScript"><meta name="author" content="娄豪杰"><meta name="copyright" content="娄豪杰"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="BOM &#x2F; DOM（上）BOM BOM（Browser Object Model）： 浏览器对象模型  其实就是操作浏览器的一些能力  我们可以操作哪些内容  获取一些浏览器的相关信息（窗口的大小）  操作浏览器进行页面跳转  获取当前浏览器地址栏的信息  操作浏览器的滚动条  浏览器的信息（浏览器的版本）  让浏览器出现一个弹出框（alert&#x2F;confirm&#x2F;prompt）    BOM 的核心">
<meta property="og:type" content="article">
<meta property="og:title" content="九、BOM和DOM">
<meta property="og:url" content="http://example.com/article/a253b181.html">
<meta property="og:site_name" content="HJ BLOG">
<meta property="og:description" content="BOM &#x2F; DOM（上）BOM BOM（Browser Object Model）： 浏览器对象模型  其实就是操作浏览器的一些能力  我们可以操作哪些内容  获取一些浏览器的相关信息（窗口的大小）  操作浏览器进行页面跳转  获取当前浏览器地址栏的信息  操作浏览器的滚动条  浏览器的信息（浏览器的版本）  让浏览器出现一个弹出框（alert&#x2F;confirm&#x2F;prompt）    BOM 的核心">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1645530798&t=e5c6ac493d1c8b04cd2a4740e1cc9870">
<meta property="article:published_time" content="2019-03-08T16:00:00.000Z">
<meta property="article:modified_time" content="2022-01-23T11:57:21.233Z">
<meta property="article:author" content="娄豪杰">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1645530798&t=e5c6ac493d1c8b04cd2a4740e1cc9870"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/article/a253b181"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '九、BOM和DOM',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-01-23 19:57:21'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/static-butterfly/dist/css/index.min.css"><link rel="stylesheet" href="/css/custom.css"><meta name="generator" content="Hexo 5.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">70</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">17</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa-fw fa fa-book"></i><span> 阅读</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa-fw fa fa-bars"></i><span> 娱乐</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/photo"><i class="fa-fw fas fa-image"></i><span> 相册</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fa-fw fas fa-heart card-announcement-animation"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&amp;refer=http%3A%2F%2Fpic1.zhimg.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1645530798&amp;t=e5c6ac493d1c8b04cd2a4740e1cc9870')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">HJ BLOG</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa-fw fa fa-book"></i><span> 阅读</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa-fw fa fa-bars"></i><span> 娱乐</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/photo"><i class="fa-fw fas fa-image"></i><span> 相册</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fa-fw fas fa-heart card-announcement-animation"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">九、BOM和DOM</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2019-03-08T16:00:00.000Z" title="发表于 2019-03-09 00:00:00">2019-03-09</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-01-23T11:57:21.233Z" title="更新于 2022-01-23 19:57:21">2022-01-23</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/JavaScript/">JavaScript</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="九、BOM和DOM"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="BOM-DOM（上）"><a href="#BOM-DOM（上）" class="headerlink" title="BOM / DOM（上）"></a>BOM / DOM（上）</h1><h2 id="BOM"><a href="#BOM" class="headerlink" title="BOM"></a>BOM</h2><ul>
<li><p>BOM（Browser Object Model）： 浏览器对象模型</p>
</li>
<li><p>其实就是操作浏览器的一些能力</p>
</li>
<li><p>我们可以操作哪些内容</p>
<ol>
<li><p>获取一些浏览器的相关信息（窗口的大小）</p>
</li>
<li><p>操作浏览器进行页面跳转</p>
</li>
<li><p>获取当前浏览器地址栏的信息</p>
</li>
<li><p>操作浏览器的滚动条</p>
</li>
<li><p>浏览器的信息（浏览器的版本）</p>
</li>
<li><p>让浏览器出现一个弹出框（alert/confirm/prompt）</p>
</li>
</ol>
</li>
<li><p>BOM 的核心就是 window 对象</p>
</li>
<li><p>window 是浏览器内置的一个对象，里面包含着操作浏览器的方法</p>
</li>
</ul>
<h3 id="弹窗"><a href="#弹窗" class="headerlink" title="弹窗"></a>弹窗</h3><ol>
<li><p><code>aleat()</code>是在浏览器弹出一个提示框</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.alert(<span class="string">&quot;我是一个提示框&quot;</span>);</span><br><span class="line"><span class="comment">// 这个弹出层知识一个提示内容，只有一个确定按钮，点击确定按钮以后，这个提示框就消失了</span></span><br></pre></td></tr></table></figure></li>
<li><p><code>confirm</code> 是在浏览器弹出一个询问框</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> boo = <span class="built_in">window</span>.confirm(<span class="string">&quot;我是一个询问框&quot;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(boo);</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">   这个弹出层有一个询问信息和两个按钮</span></span><br><span class="line"><span class="comment">   当你点击确定的时候，就会得到 true，当你点击取消的时候，就会得到 false</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure></li>
<li><p><code>prompt</code> 是在浏览器弹出一个输入框</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="built_in">window</span>.prompt(<span class="string">&quot;请输入内容：&quot;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(str);</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">   这个弹出层有一个输入框和两个按钮</span></span><br><span class="line"><span class="comment">   当你点击取消的时候，得到的是 null</span></span><br><span class="line"><span class="comment">   当你点击确定的时候得到的就是你输入的内容</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h3 id="获取浏览器窗口的尺寸"><a href="#获取浏览器窗口的尺寸" class="headerlink" title="获取浏览器窗口的尺寸"></a>获取浏览器窗口的尺寸</h3></li>
</ol>
<ul>
<li><p><code> innerHeight</code> 和 <code>innerWidth</code>，这两个方法分别是用来获取浏览器窗口的宽度和高度（包含滚动条的）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> windowHeight = <span class="built_in">window</span>.innerHeight;</span><br><span class="line"><span class="built_in">console</span>.log(windowHeight);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> windowWidth = <span class="built_in">window</span>.innerWidth;</span><br><span class="line"><span class="built_in">console</span>.log(windowWidth);</span><br></pre></td></tr></table></figure>
<h3 id="浏览器地址信息"><a href="#浏览器地址信息" class="headerlink" title="浏览器地址信息"></a>浏览器地址信息</h3></li>
<li><p>在 window 中有一个对象叫做 <code>location</code></p>
</li>
<li><p>就是专门用来存储浏览器的地址栏内的信息的</p>
</li>
</ul>
<ol>
<li><p><code>window.location.href</code>，这个属性存储的是浏览器地址栏内 url 地址的信息</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">window</span>.location.href); <span class="comment">// https://www.baidu.com/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 这个属性也可以给他赋值</span></span><br><span class="line"><span class="built_in">window</span>.location.href = <span class="string">&quot;https://www.jd.com/&quot;</span>; <span class="comment">// 这个就会跳转页面到后面你给的那个地址</span></span><br></pre></td></tr></table></figure></li>
<li><p><code>window.location.reload()</code>，这个方法会重新加载一遍页面，就相当于刷新是一个道理</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.location.reload();</span><br><span class="line"><span class="comment">// 注意：不要写在全局，不然浏览器就会一直处在刷新状态</span></span><br></pre></td></tr></table></figure>
<h3 id="浏览器的历史记录"><a href="#浏览器的历史记录" class="headerlink" title="浏览器的历史记录"></a>浏览器的历史记录</h3></li>
</ol>
<ul>
<li>window 中有一个对象叫做 <code>history</code></li>
<li>是专门用来存储历史记录信息的</li>
</ul>
<ol>
<li><p><code>history.back</code>，是用来会退历史记录的，就是回到前一个页面，就相当于浏览器上的 ⬅️ 按钮</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.history.back();</span><br><span class="line"><span class="comment">// 前提是你要有上一条记录，不然就是一直在这个页面，也不会回退</span></span><br></pre></td></tr></table></figure></li>
<li><p><code>history.forword</code>，是去到下一个历史记录里面，也就是去到下一个页面，就相当于浏览器上的 ➡️ 按钮</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.history.forward();</span><br><span class="line"><span class="comment">// 前提是你要之前有过回退操作，不然的话你现在就是最后一个页面，没有下一个</span></span><br></pre></td></tr></table></figure>
<h3 id="浏览器的版本信息"><a href="#浏览器的版本信息" class="headerlink" title="浏览器的版本信息"></a>浏览器的版本信息</h3></li>
</ol>
<ul>
<li>window 中有一个对象叫做 <code>navigator</code></li>
<li>是专门用来获取浏览器信息的</li>
</ul>
<ol>
<li><p><code>navigator.userAgent</code>，是获取的浏览器的整体信息</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">window</span>.navigator.userAgent);</span><br><span class="line"><span class="comment">// Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36</span></span><br></pre></td></tr></table></figure></li>
<li><p><code>navigator.appName</code>， 获取的是浏览器的名称</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">window</span>.navigator.appName);</span><br></pre></td></tr></table></figure></li>
<li><p><code>navigator.appVersion</code>，获取的是浏览器的版本号</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">window</span>.navigator.appVersion);</span><br></pre></td></tr></table></figure></li>
<li><p><code>navigator.platform</code>，获取到的是当前计算机的操作系统</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">console.log(window.navigator.platform);</span><br></pre></td></tr></table></figure>
<h3 id="浏览器的-onload-事件"><a href="#浏览器的-onload-事件" class="headerlink" title="浏览器的 onload 事件"></a>浏览器的 onload 事件</h3></li>
</ol>
<ul>
<li><p>这个不在是对象了，而是一个事件，是在页面所有资源加载完毕后执行的</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.onload = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;页面已经加载完毕触发的&quot;</span>);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<ul>
<li><strong>使用场景：</strong></li>
</ul>
<ol>
<li><p>在 html 页面中把 js 写在 head 里面</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">      <span class="comment">// 这个代码执行的时候，body 还没有加载</span></span></span><br><span class="line"><span class="javascript">      <span class="comment">// 这个时候我们就获取不到 body 中的那个 div</span></span></span><br><span class="line"></span><br><span class="line"><span class="javascript">      <span class="comment">// 就需要使用 window.onload 事件</span></span></span><br><span class="line"><span class="javascript">      <span class="built_in">window</span>.onload = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 这个函数会在页面加载完毕以后在执行</span></span></span><br><span class="line"><span class="javascript">        <span class="comment">// 那么这个时候页面的 DOM 元素都已经加载了，我们就可以获取 div 了</span></span></span><br><span class="line">      &#125;;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p>在 html 页面中把 js 写在 body 最后面</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span> /&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">      <span class="comment">// 这个代码执行的时候，body 已经加载完毕了</span></span></span><br><span class="line"><span class="javascript">      <span class="comment">// 在这里就可以获取到 div，写不写 window.onload 就无所谓了</span></span></span><br><span class="line"></span><br><span class="line"><span class="javascript">      <span class="built_in">window</span>.onload = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 这个函数会在页面加载完毕以后在执行</span></span></span><br><span class="line"><span class="javascript">        <span class="comment">// 那么这个时候页面的 DOM 元素都已经加载了，我们就可以获取 div 了</span></span></span><br><span class="line">      &#125;;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="浏览器的-onscroll-事件"><a href="#浏览器的-onscroll-事件" class="headerlink" title="浏览器的 onscroll 事件"></a>浏览器的 onscroll 事件</h3></li>
</ol>
</li>
<li><p>这个 onscroll 事件是当浏览器的滚动条滚动的时候触发</p>
</li>
<li><p>或者鼠标滚轮滚动的时候出发</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.onscroll = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;浏览器滚动了&quot;</span>);</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 注意：前提是页面的高度要超过浏览器的可是窗口才可以</span></span><br></pre></td></tr></table></figure></li>
<li><p><strong>浏览器滚动的距离:</strong></p>
</li>
</ul>
<ol>
<li><p><code>scrollTop</code>，获取的是页面向上滚动的距离</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 一共有两个获取方式</span></span><br><span class="line">-<span class="built_in">document</span>.body.scrollTop - <span class="built_in">document</span>.documentElement.scrollTop;</span><br><span class="line"></span><br><span class="line"><span class="built_in">window</span>.onscroll = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">document</span>.body.scrollTop);</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">document</span>.documentElement.scrollTop);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">  两种方式的区别：</span></span><br><span class="line"><span class="comment">  IE浏览器：</span></span><br><span class="line"><span class="comment">  		)没有 `DOCTYPE` 声明的时候，用这两个都行</span></span><br><span class="line"><span class="comment"> 		)有 `DOCTYPE` 声明的时候，只能用 `document.documentElement.scrollTop`</span></span><br><span class="line"><span class="comment"> 		</span></span><br><span class="line"><span class="comment">  Chrome 和 FireFox：</span></span><br><span class="line"><span class="comment">  		)没有 `DOCTYPE` 声明的时候，用 `document.body.scrollTop`</span></span><br><span class="line"><span class="comment">  		)有 `DOCTYPE` 声明的时候，用 `document.documentElement.scrollTop`</span></span><br><span class="line"><span class="comment">  Safari：</span></span><br><span class="line"><span class="comment">  		)两个都不用，使用一个单独的方法 `window.pageYOffset `</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure></li>
<li><p><code>scrollLeft</code>，获取页面向左滚动的距离</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 一共有两个获取方式</span></span><br><span class="line">-<span class="built_in">document</span>.body.scrollLeft - <span class="built_in">document</span>.documentElementLeft;</span><br><span class="line"></span><br><span class="line"><span class="built_in">window</span>.onscroll = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">document</span>.body.scrollLeft);</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">document</span>.documentElement.scrollLeft);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="DOM（上）"><a href="#DOM（上）" class="headerlink" title="DOM（上）"></a>DOM（上）</h2></li>
</ol>
<ul>
<li>DOM（Document Object Model）： 文档对象模型</li>
<li>其实就是操作 html 中的标签的一些能力</li>
<li>我们可以操作哪些内容<ul>
<li>获取一个元素</li>
<li>移除一个元素</li>
<li>创建一个元素</li>
<li>向页面里面添加一个元素</li>
<li>给元素绑定一些事件</li>
<li>获取元素的属性</li>
<li>给元素添加一些 css 样式</li>
</ul>
</li>
<li>DOM 的核心对象就是 docuemnt 对象</li>
<li>document 对象是浏览器内置的一个对象，里面存储着专门用来操作元素的各种方法</li>
<li>DOM： 页面中的标签，我们通过 js 获取到以后，就把这个对象叫做 DOM 对象</li>
</ul>
<h3 id="获取-dom-元素常用方式"><a href="#获取-dom-元素常用方式" class="headerlink" title="获取 dom 元素常用方式"></a>获取 dom 元素常用方式</h3><ol>
<li><p><code>getElementById</code>，是通过标签的 id 名称来获取标签的</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">// 因为在一个页面中 id 是唯一的，所以获取到的就是一个元素</span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;box&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> box = <span class="built_in">document</span>.getElementById(<span class="string">&quot;box&quot;</span>);</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(box); <span class="comment">// &lt;div&gt;&lt;/div&gt;</span></span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p><code>getElementsByClassName</code>，是用过标签的 class 名称来获取标签的</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">/* 因为页面中可能有多个元素的 class 名称一样，所以获取到的是一组元素</span><br><span class="line">哪怕你获取的 class 只有一个，那也是获取一组元素，只不过这一组中只有一个 DOM</span><br><span class="line">元素而已 */</span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">calss</span>=<span class="string">&quot;box&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> box = <span class="built_in">document</span>.getElementsByClassName(<span class="string">&quot;box&quot;</span>);</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(box); <span class="comment">// [&lt;div&gt;&lt;/div&gt;]</span></span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(box[<span class="number">0</span>]); <span class="comment">// &lt;div&gt;&lt;/div&gt;</span></span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p><code>getElementsByTagName</code>，是用过标签的 标签 名称来获取标签的</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">/* 因为页面中可能有多个元素的 标签 名称一样，所以获取到的是一组元素</span><br><span class="line">哪怕真的只有一个这个标签名，那么也是获取一组元素，只不过这一组中只有一个 DOM</span><br><span class="line">元素而已 */</span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> box = <span class="built_in">document</span>.getElementsByTagName(<span class="string">&quot;div&quot;</span>);</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(box); <span class="comment">// [&lt;div&gt;&lt;/div&gt;]</span></span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(box[<span class="number">0</span>]); <span class="comment">// &lt;div&gt;&lt;/div&gt;</span></span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p><code>querySelector</code>，是按照选择器的方式来获取元素</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">  也就是说，按照我们写 css 的时候的选择器来获取</span></span><br><span class="line"><span class="comment">  这个方法只能获取到一个元素，并且是页面中第一个满足条件的元素</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">document</span>.querySelector(<span class="string">&quot;div&quot;</span>)); <span class="comment">// 获取页面中的第一个 div 元素</span></span><br><span class="line"><span class="built_in">console</span>.log(docuemnt.querySelector(<span class="string">&quot;.box&quot;</span>)); <span class="comment">// 获取页面中第一个有 box 类名的元素</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">document</span>.querySelector(<span class="string">&quot;#box&quot;</span>)); <span class="comment">// 获取页面中第一个 id 名为 box 的元素</span></span><br></pre></td></tr></table></figure></li>
<li><p><code>querySelectorAll</code>，是按照选择器的方式来获取元素</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 这个方法能获取到所有满足条件的元素，以一个伪数组的形式返回</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">document</span>.querySelectorAll(<span class="string">&quot;div&quot;</span>)); <span class="comment">// 获取页面中的所有的 div 元素</span></span><br><span class="line"><span class="built_in">console</span>.log(docuemnt.querySelectorAll(<span class="string">&quot;.box&quot;</span>)); <span class="comment">// 获取页面中所有有 box 类名的元素</span></span><br></pre></td></tr></table></figure>
<h3 id="操作-dom-属性"><a href="#操作-dom-属性" class="headerlink" title="操作 dom 属性"></a>操作 dom 属性</h3></li>
</ol>
<ul>
<li>通过我们各种获取元素的方式获取到页面中的标签以后</li>
<li>我们可以直接操作 DOM 元素的属性，就能直接把效果展示在页面上</li>
</ul>
<ol>
<li><p><code>innerHTML</code>，获取元素内部的 HTML 结构</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 第一种使用方式是：获取dom元素内部的结构 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>hello world<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> div = <span class="built_in">document</span>.querySelector(<span class="string">&quot;div&quot;</span>);</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(div.innerHTML);</span></span><br><span class="line"><span class="handlebars"><span class="xml">    /* <span class="tag">&lt;<span class="name">p</span>&gt;</span>hello world<span class="tag">&lt;/<span class="name">p</span>&gt;</span> */</span></span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 第二种使用方式是设置dom元素的内容 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> div = <span class="built_in">document</span>.querySelector(<span class="string">&quot;div&quot;</span>);</span></span><br><span class="line"><span class="handlebars"><span class="xml">    div.innerHTML = &quot;<span class="tag">&lt;<span class="name">p</span>&gt;</span>hello<span class="tag">&lt;/<span class="name">p</span>&gt;</span>&quot;;</span></span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p><code>innerText</code>，获取元素内部的文本（只能获取到文本内容，获取不到 html 标签）</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 第一种使用方式是：获取dom元素内部的文本 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>hello world<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> div = <span class="built_in">document</span>.querySelector(<span class="string">&quot;div&quot;</span>);</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(div.innerText);</span></span><br><span class="line"><span class="javascript">    <span class="comment">/* hello world */</span></span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 第二种使用方式是设置dom元素的内容 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> div = <span class="built_in">document</span>.querySelector(<span class="string">&quot;div&quot;</span>);</span></span><br><span class="line"><span class="javascript">    div.innerText = <span class="string">&quot;hello&quot;</span>;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p><code>getAttribute</code>，获取元素的某个属性（包括自定义属性）</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">a</span>=<span class="string">&quot;100&quot;</span> <span class="attr">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> div = <span class="built_in">document</span>.querySelector(<span class="string">&quot;div&quot;</span>);</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(div.getAttribute(<span class="string">&quot;a&quot;</span>)); <span class="comment">// 100</span></span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(div.getAttribute(<span class="string">&quot;class&quot;</span>)); <span class="comment">// box</span></span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p><code>setAttribute</code>，给元素设置一个属性（包括自定义属性）</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> div = <span class="built_in">document</span>.querySelector(<span class="string">&quot;div&quot;</span>);</span></span><br><span class="line"><span class="javascript">    div.setAttribute(<span class="string">&quot;a&quot;</span>, <span class="number">100</span>);</span></span><br><span class="line"><span class="javascript">    div.setAttribute(<span class="string">&quot;class&quot;</span>, <span class="string">&quot;box&quot;</span>);</span></span><br><span class="line"><span class="handlebars"><span class="xml">    console.log(div); // <span class="tag">&lt;<span class="name">div</span> <span class="attr">a</span>=<span class="string">&quot;100&quot;</span> <span class="attr">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p><code>removeAttribute</code>，直接移除元素的某个属性</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">a</span>=<span class="string">&quot;100&quot;</span> <span class="attr">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> div = <span class="built_in">document</span>.querySelector(<span class="string">&quot;div&quot;</span>);</span></span><br><span class="line"><span class="javascript">    div.removeAttribute(<span class="string">&quot;class&quot;</span>);</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(div); <span class="comment">// &lt;div a=&quot;100&quot;&gt;&lt;/div&gt;</span></span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p><code>style</code>，专门用来给元素添加 css 样式的（添加的都是行内样式）</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> div = <span class="built_in">document</span>.querySelector(<span class="string">&quot;div&quot;</span>);</span></span><br><span class="line"><span class="javascript">    div.style.width = <span class="string">&quot;100px&quot;</span>;</span></span><br><span class="line"><span class="javascript">    div.style.height = <span class="string">&quot;100px&quot;</span>;</span></span><br><span class="line"><span class="javascript">    div.style.backgroundColor = <span class="string">&quot;pink&quot;</span>;</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(div);</span></span><br><span class="line"><span class="handlebars"><span class="xml">    // <span class="tag">&lt;<span class="name">div</span> <span class="attr">style</span>=<span class="string">&quot;width: 100px; height: 100px; background-color: pink;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p><code>className</code>，专门用来操作元素的 类名的</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 获取dom元素身上有哪些类名 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> div = <span class="built_in">document</span>.querySelector(<span class="string">&quot;div&quot;</span>);</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(div.className); <span class="comment">// box</span></span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- </span></span><br><span class="line"><span class="comment">	也可以设置元素的类名，不过是全覆盖式的操作 </span></span><br><span class="line"><span class="comment">	在设置的时候，不管之前有没有类名，都会全部被设置的值覆盖</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> div = <span class="built_in">document</span>.querySelector(<span class="string">&quot;div&quot;</span>);</span></span><br><span class="line"><span class="javascript">    div.className = <span class="string">&quot;test&quot;</span>;</span></span><br><span class="line"><span class="handlebars"><span class="xml">    console.log(div); // <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;test&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
</ol>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">娄豪杰</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://example.com/article/a253b181.html">http://example.com/article/a253b181.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">HJ BLOG</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/JavaScript/">JavaScript</a></div><div class="post_share"><div class="social-share" data-image="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&amp;refer=http%3A%2F%2Fpic1.zhimg.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1645530798&amp;t=e5c6ac493d1c8b04cd2a4740e1cc9870" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/article/5883fa92.html"><img class="prev-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&amp;refer=http%3A%2F%2Fpic1.zhimg.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1645530798&amp;t=e5c6ac493d1c8b04cd2a4740e1cc9870" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">十、DOM（下）</div></div></a></div><div class="next-post pull-right"><a href="/article/318dac24.html"><img class="next-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&amp;refer=http%3A%2F%2Fpic1.zhimg.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1645530798&amp;t=e5c6ac493d1c8b04cd2a4740e1cc9870" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">八、Math和Date</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/article/23cb5f5f.html" title="一、初识JavaScript"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1645530798&t=e5c6ac493d1c8b04cd2a4740e1cc9870" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-01</div><div class="title">一、初识JavaScript</div></div></a></div><div><a href="/article/4200b2b0.html" title="三、JS循环结构"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1645530798&t=e5c6ac493d1c8b04cd2a4740e1cc9870" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-03</div><div class="title">三、JS循环结构</div></div></a></div><div><a href="/article/2c83c14b.html" title="四、JS函数（上）"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1645530798&t=e5c6ac493d1c8b04cd2a4740e1cc9870" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-04</div><div class="title">四、JS函数（上）</div></div></a></div><div><a href="/article/bdf08e07.html" title="二、JS分支结构"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1645530798&t=e5c6ac493d1c8b04cd2a4740e1cc9870" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-02</div><div class="title">二、JS分支结构</div></div></a></div><div><a href="/article/dcee35d7.html" title="五、JS函数（下）"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1645530798&t=e5c6ac493d1c8b04cd2a4740e1cc9870" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-05</div><div class="title">五、JS函数（下）</div></div></a></div><div><a href="/article/23ec2cd1.html" title="六、JS数组[Array]"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1645530798&t=e5c6ac493d1c8b04cd2a4740e1cc9870" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-06</div><div class="title">六、JS数组[Array]</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">娄豪杰</div><div class="author-info__description"></div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">70</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">17</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/louhaojie99"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">人生就像品茗，懂得吃苦，才能回甘啊。</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#BOM-DOM%EF%BC%88%E4%B8%8A%EF%BC%89"><span class="toc-number">1.</span> <span class="toc-text">BOM &#x2F; DOM（上）</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#BOM"><span class="toc-number">1.1.</span> <span class="toc-text">BOM</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%B9%E7%AA%97"><span class="toc-number">1.1.1.</span> <span class="toc-text">弹窗</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E6%B5%8F%E8%A7%88%E5%99%A8%E7%AA%97%E5%8F%A3%E7%9A%84%E5%B0%BA%E5%AF%B8"><span class="toc-number">1.1.2.</span> <span class="toc-text">获取浏览器窗口的尺寸</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8F%E8%A7%88%E5%99%A8%E5%9C%B0%E5%9D%80%E4%BF%A1%E6%81%AF"><span class="toc-number">1.1.3.</span> <span class="toc-text">浏览器地址信息</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9A%84%E5%8E%86%E5%8F%B2%E8%AE%B0%E5%BD%95"><span class="toc-number">1.1.4.</span> <span class="toc-text">浏览器的历史记录</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9A%84%E7%89%88%E6%9C%AC%E4%BF%A1%E6%81%AF"><span class="toc-number">1.1.5.</span> <span class="toc-text">浏览器的版本信息</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9A%84-onload-%E4%BA%8B%E4%BB%B6"><span class="toc-number">1.1.6.</span> <span class="toc-text">浏览器的 onload 事件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9A%84-onscroll-%E4%BA%8B%E4%BB%B6"><span class="toc-number">1.1.7.</span> <span class="toc-text">浏览器的 onscroll 事件</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#DOM%EF%BC%88%E4%B8%8A%EF%BC%89"><span class="toc-number">1.2.</span> <span class="toc-text">DOM（上）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96-dom-%E5%85%83%E7%B4%A0%E5%B8%B8%E7%94%A8%E6%96%B9%E5%BC%8F"><span class="toc-number">1.2.1.</span> <span class="toc-text">获取 dom 元素常用方式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%93%8D%E4%BD%9C-dom-%E5%B1%9E%E6%80%A7"><span class="toc-number">1.2.2.</span> <span class="toc-text">操作 dom 属性</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/article/cc11b2b0.html" title="二、JavaScript设计模式【下】"><img src="https://picabstract-preview-ftn.weiyun.com/ftn_pic_abs_v3/983a2ddf4c01d0f319b391469f669eba7a1a82ba077da903e3114d4062c26cdea809b197d9cf02bb077af6272de8e1ed?pictype=scale&amp;from=30113&amp;version=3.3.3.3&amp;uin=851681631&amp;fname=12_js.jpg&amp;size=750" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="二、JavaScript设计模式【下】"/></a><div class="content"><a class="title" href="/article/cc11b2b0.html" title="二、JavaScript设计模式【下】">二、JavaScript设计模式【下】</a><time datetime="2021-09-11T16:00:00.000Z" title="发表于 2021-09-12 00:00:00">2021-09-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/article/cc00b2b0.html" title="一、JavaScript设计模式【上】"><img src="https://picabstract-preview-ftn.weiyun.com/ftn_pic_abs_v3/983a2ddf4c01d0f319b391469f669eba7a1a82ba077da903e3114d4062c26cdea809b197d9cf02bb077af6272de8e1ed?pictype=scale&amp;from=30113&amp;version=3.3.3.3&amp;uin=851681631&amp;fname=12_js.jpg&amp;size=750" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="一、JavaScript设计模式【上】"/></a><div class="content"><a class="title" href="/article/cc00b2b0.html" title="一、JavaScript设计模式【上】">一、JavaScript设计模式【上】</a><time datetime="2021-09-10T16:00:00.000Z" title="发表于 2021-09-11 00:00:00">2021-09-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/article/vue-sound-code10.html" title="十、Vue源码【内置组件篇】"><img src="https://img2.baidu.com/it/u=4067570870,2963075030&amp;fm=253&amp;fmt=auto&amp;app=120&amp;f=JPEG?w=640&amp;h=360" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="十、Vue源码【内置组件篇】"/></a><div class="content"><a class="title" href="/article/vue-sound-code10.html" title="十、Vue源码【内置组件篇】">十、Vue源码【内置组件篇】</a><time datetime="2021-09-09T16:00:00.000Z" title="发表于 2021-09-10 00:00:00">2021-09-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/article/vue-sound-code9.html" title="九、Vue源码【指令篇】"><img src="https://img2.baidu.com/it/u=4067570870,2963075030&amp;fm=253&amp;fmt=auto&amp;app=120&amp;f=JPEG?w=640&amp;h=360" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="九、Vue源码【指令篇】"/></a><div class="content"><a class="title" href="/article/vue-sound-code9.html" title="九、Vue源码【指令篇】">九、Vue源码【指令篇】</a><time datetime="2021-09-08T16:00:00.000Z" title="发表于 2021-09-09 00:00:00">2021-09-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/article/vue-sound-code8.html" title="八、Vue源码【过滤器篇】"><img src="https://img2.baidu.com/it/u=4067570870,2963075030&amp;fm=253&amp;fmt=auto&amp;app=120&amp;f=JPEG?w=640&amp;h=360" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="八、Vue源码【过滤器篇】"/></a><div class="content"><a class="title" href="/article/vue-sound-code8.html" title="八、Vue源码【过滤器篇】">八、Vue源码【过滤器篇】</a><time datetime="2021-09-07T16:00:00.000Z" title="发表于 2021-09-08 00:00:00">2021-09-08</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&amp;refer=http%3A%2F%2Fpic1.zhimg.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1645530798&amp;t=e5c6ac493d1c8b04cd2a4740e1cc9870')"><div id="footer-wrap"><div class="copyright">&copy;2017 - 2022 By 娄豪杰</div><div class="footer_custom_text"><div>嗨，欢迎来到我的博客，希望能帮助到您！</div></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"></div><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-show-text.min.js" data-mobile="false" data-text="富强,民主,文明,和谐,自由,平等,公正,法治" data-fontsize="15px" data-random="true" async="async"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = [
  'title',
  '#config-diff',
  '#body-wrap',
  '#rightside-config-hide',
  '#rightside-config-show',
  '.js-pjax'
]

if (false) {
  pjaxSelectors.unshift('meta[property="og:image"]', 'meta[property="og:title"]', 'meta[property="og:url"]')
}

var pjax = new Pjax({
  elements: 'a:not([target="_blank"]):not([href="/music/"]):not([href="/no-pjax/"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener toc scroll 
  window.removeEventListener('scroll', window.tocScrollFn)

  typeof preloader === 'object' && preloader.initLoading()
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // Analytics
  if (false) {
    MtaH5.pgv()
  }

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":150,"height":300},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>